
import 'package:geolocator/geolocator.dart';

class GeoUtils {

  static String getMessage(double code) {
    if (code == 1) {
      return "位置权限被拒绝，请授予位置权限";
    }
    if (code == 2) {
      return "位置权限被永久拒绝，请手动启用";
    }
     if (code == -1) {
      return "获取位位置服务未启用，请启用位置服务";
    }
    if (code == -2) {
      return "获取位置失败，请稍后重试";
    }
    return "获取位成功";
  }

  static Future<List<double>> getPosition() async {
    List<double> result = [0, 0, 0];
    try {
      bool isServiceEnabled = await Geolocator.isLocationServiceEnabled();
      if (!isServiceEnabled) {
        result[0] = -1;
        return result;
      }
      LocationPermission permission = await Geolocator.checkPermission();
      if (permission == LocationPermission.denied) {
        permission = await Geolocator.requestPermission();
        if (permission == LocationPermission.denied) {
          result[0] = 1;
          return result;
        }
      }
      if (permission == LocationPermission.deniedForever) {
        result[0] = 2;
        return result;
      }
      Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high,
      );
      result[1] = position.latitude;
      result[2] = position.longitude;
    } catch (e) {
      result[0] = -2;
    }

    return result;
  }
}
